package com.barradata.common.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.barradata.R
import com.barradata.app0624.modules.web.EventType
import com.barradata.app0624.modules.web.WebCallbackEvent
import com.barradata.common.rxbus.RxBus
import com.example.hfiveandaplication.dialog.LoadingDialog
import com.google.android.material.snackbar.Snackbar
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.login_debug_activity.*
import com.barradata.BuildConfig


/**
 * Author:    WuNan
 * Email:     wunan@barradata.com
 * Created:   2019/7/4 14:57
 **/
abstract class BaseActivity : AppCompatActivity(), BaseComponent {
    // 在 BaseActivity 中统一维护 Rx 带来的 disposable，避免内存泄漏
    override fun compositeDisposable() = CompositeDisposable()

    private lateinit var loadingDialog: LoadingDialog

    abstract fun getLayoutId(): Int

    abstract fun initView()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadingDialog = LoadingDialog(this, R.style.loading_dialog)
        setContentView(getLayoutId())
        initView()
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        lifecycleLog("onCreate")
    }

    override fun onStart() {
        super.onStart()
        lifecycleLog("onStart")
    }

    override fun onRestart() {
        super.onRestart()
        lifecycleLog("onRestart")
    }

    override fun onResume() {
        super.onResume()
        lifecycleLog("onResume")
    }

    override fun onPause() {
        super.onPause()
        lifecycleLog("onPause")
    }

    override fun onStop() {
        super.onStop()
        lifecycleLog("onStop");
    }

    override fun onDestroy() {
        disposeExt()
        super.onDestroy()
        lifecycleLog("onDestroy")
    }

    fun showLoadingView() {
        if (!loadingDialog.isShowing()) {
            loadingDialog.showLoading()
        }
    }

    fun dismissLoadingView() {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismissLoading()
        }
    }

    protected fun getStringResource(strResourceId: Int): String {
        return resources.getString(strResourceId)
    }

    private fun showSnackBar(content: String, @StringRes contentResId: Int, duration: Int) {
        if (content.isNotEmpty())
            Snackbar.make(findViewById(android.R.id.content), content, duration).show()
        else if (contentResId != 0)
            Snackbar.make(findViewById(android.R.id.content), contentResId, duration).show()
    }

    @JvmOverloads
    fun showSnackBarShort(content: String = "", @StringRes contentResId: Int = 0) {
        showSnackBar(content, contentResId, Snackbar.LENGTH_SHORT)
    }

    @JvmOverloads
    fun showSnackBarLong(content: String = "", @StringRes contentResId: Int = 0) {
        showSnackBar(content, contentResId, Snackbar.LENGTH_LONG)
    }

    fun setupActionBar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                RxBus.post(WebCallbackEvent(EventType.FROM_CANCEL))
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun getClassName(): String {
        return this.javaClass.simpleName
    }

    private fun lifecycleLog(action: String) {
        if (BuildConfig.DEBUG) {
            Log.v("lifecycle", "---" + getClassName() + "---" + action)
        }
    }
}

